package com.youhome.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.BMessageMapper;
import com.youhome.service.model.BMessage;
import com.youhome.service.model.Principal;
import com.youhome.service.parameter.ParamMessageSend;
import com.youhome.service.service.BMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2017/12/4.
 */
@Service
public class BMessageServiceImpl implements BMessageService {
    @Autowired
    private BMessageMapper bMessageMapper;

    @Override
    @Transactional
    public int send(Long sender, List<String> receivers, String title, String content) {
        BMessage message = new BMessage();

        for (String receiver : receivers) {
            message.setSender(sender);
            message.setReceiver(Long.valueOf(receiver));
            message.setTitle(title);
            message.setContent(content);
            message.setStatus(0);
            message.setCtime(new Date());
            bMessageMapper.insertSelective(message);
        }

        return 1;
    }

    @Override
    public PageInfo<BMessage> selectByBean(Integer page, Integer pageSize, BMessage record) {
        if (page == null || page < 1)
            page = 1;

        if (pageSize == null || pageSize < 1)
            pageSize = 10;

        PageHelper.startPage(page, pageSize);
        List<BMessage> list = bMessageMapper.selectByBean(record);

        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<BMessage> selectByBean(Principal principal, Integer status, Integer pageNumber, Integer pageSize) throws CustomException {
        Long userId = principal.getUser().getUserId();

        BMessage record = new BMessage();
        record.setReceiver(userId);
        if (!StringUtils.isEmpty(status)) {
            record.setStatus(status);
        }
        record.setRstatus(true);

        if (pageNumber == null || pageNumber < 1)
            pageNumber = 1;

        if (pageSize == null || pageSize < 1)
            pageSize = 10;

        PageHelper.startPage(pageNumber, pageSize);
        List<BMessage> list = bMessageMapper.selectByBean(record);

        return new PageInfo<>(list);

    }

    @Override
    public int selectCount(BMessage message) {
        return bMessageMapper.selectCountByBean(message);
    }

    @Override
    public BMessage selectByPrimaryKeyAndReceiver(BMessage record) {
        return bMessageMapper.selectByPrimaryKeyAndReceiver(record);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return bMessageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByBean(BMessage record) {
        return bMessageMapper.deleteByBean(record);
    }

    @Override
    public int update(BMessage message) {
        return bMessageMapper.updateByPrimaryKeySelective(message);
    }

    @Override
    public int updateStatus(BMessage message) {
        return bMessageMapper.updateStatus(message);
    }

    @Override
    public BMessage selectByPrimaryKey(Long id) throws CustomException {
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        return bMessageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(BMessage message) {
        return bMessageMapper.updateByPrimaryKeySelective(message);
    }

    @Override
    @Transactional
    public void send(Principal principal, ParamMessageSend map) throws CustomException {

        String receivers = map.getReceivers();
        if (StringUtils.isEmpty(receivers)) {
            throw new CustomException(ResultCode.PLEASE_SELECT_THE_RECIPIENT);
        }

        List<String> arr_receiver = Arrays.asList(receivers.split(","));

        if (StringUtils.isEmpty(map.getTitle())) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        if (StringUtils.isEmpty(map.getContent())) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }


        Long sender_UserId = principal.getUser().getUserId();

        BMessage message = new BMessage();
        for (String receiver : arr_receiver) {
            message.setSender(sender_UserId);
            message.setReceiver(Long.valueOf(receiver));
            message.setTitle(map.getTitle());
            message.setContent(map.getContent());
            message.setStatus(0);
            message.setCtime(new Date());
            bMessageMapper.insertSelective(message);
        }
    }

    @Override
    public int selectCount(Principal principal, Integer status) throws CustomException {

        Long userId = principal.getUser().getUserId();

        BMessage message = new BMessage();
        message.setReceiver(userId);
        if (!StringUtils.isEmpty(status)) {
            message.setStatus(status);
        } else {
            message.setStatus(0);  //未读数量
        }
        message.setRstatus(true);
        return bMessageMapper.selectCountByBean(message);
    }

    @Override
    public void delete(Principal principal, Long id, Integer type) throws CustomException {
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        BMessage message = new BMessage();

        message.setId(id);
        Long userId = principal.getUser().getUserId();
        if (type == 0) {
            message.setSender(userId);
            message.setSstatus(false); //发送者
        } else if (type == 1) {
            message.setReceiver(userId);
            message.setRstatus(false); //接收者
        } else {
            throw new CustomException(ResultCode.DO_NOT_HAVE_PERMISSION);
        }
        if (bMessageMapper.updateByPrimaryKeySelective(message) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }

    @Override
    public void updateStatus(Principal principal,Long id, Integer status) throws CustomException {
        Long userId = principal.getUser().getUserId();

        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        BMessage message = new BMessage();

        message.setId(id);
        message.setReceiver(userId);
        message.setStatus(status);
        if (bMessageMapper.updateStatus(message) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }
}
